//
//  MqInfoMoreViewController.m
//  Meiqi
//
//  Created by 王郑 on 16/1/19.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqInfoMoreViewController.h"
#import "MqInfoListModel.h"
#import "MqInfoListTableViewCell.h"
#import "MqInfoWebViewController.h"
@interface MqInfoMoreViewController ()<UITableViewDelegate,UITableViewDataSource>

// 数据源数组
@property (strong, nonatomic) NSMutableArray *listArray;

@property (strong, nonatomic) UITableView *tableview;


@end

@implementation MqInfoMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
#pragma maek ----- 数据源数组
    _listArray = [NSMutableArray array];
    
#pragma maek ----- 自定义导航栏
    [self addNavBar];
    
#pragma mark ----- 解析数据
    [self setUpMoreData];
    
#pragma mark ----- tableview
    [self addTableview];
    
}
- (void)addNavBar
{
    
    UIView *barView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64 * kHeightBase)];
    barView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:barView];
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(10 * kWidthBase, 30 * kHeightBase, 20 * kWidthBase, 20 * kHeightBase);
    [backButton setBackgroundImage:[UIImage imageNamed:@"info-back"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [barView addSubview:backButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 300)/2, 20 * kHeightBase, 300*kWidthBase, 40*kHeightBase)];
    titleLabel.text = _titleString;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
    [barView addSubview:titleLabel];
    
}

- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)setUpMoreData
{
    
    [PKRequestManager requestWithUrlString:_moreURL parDic:nil Method:GET finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *list = dic[@"list"];
        for (NSDictionary *oneDic in list) {
            
            MqInfoListModel *model = [[MqInfoListModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_listArray addObject:model];
            
        }
        
        [_tableview reloadData];
        
        
    } error:^(NSError *error) {
//        NSLog(@"%@",error);
        
    } path:@"moreData"];
    
    
    
}

- (void)addTableview
{
    
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64*kHeightBase, kScreenWidth, kScreenHeight - 64*kHeightBase)];
    
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.rowHeight = 80 * kHeightBase;
    [self.view addSubview:_tableview];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"moreCell";
    MqInfoListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        
        cell = [[MqInfoListTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    MqInfoListModel *model = _listArray[indexPath.row];
    cell.model = model;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MqInfoWebViewController *webView = [[MqInfoWebViewController alloc] init];
    
    MqInfoListModel *model = _listArray[indexPath.row];
    NSString *ids = model.topicDetailID;
    NSString *classID = model.classid;
    
    webView.idsURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&&classid=1&id=%@&userid=682480&version=1",ids];
    
    // 标签的网址
    webView.tagListURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=getArticleTagOrTagListByType&type=tagList&id=%@",ids];
    
    // 作者信息的网址
    webView.authorInfoURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=getAuthorInfByType&type=authorInfo&classid=%@&id=%@",classID,ids];
    
    // 相关文章的网址
    webView.recommendListURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=render&version=1&platform=1&classid=%@&showType=recommendList",classID];
    // 评论的网址
    webView.commentURL = [NSString stringWithFormat:@"http://www.biketo.com/app.php?m=client&a=comment&methodType=getArticleCommentV1&access_token=&classid=%@&id=%@",classID,ids];
    
    [self.navigationController pushViewController:webView animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
